package com.ouli.crm.settings.servce.impl;

import com.ouli.crm.exception.LoginException;
import com.ouli.crm.settings.dao.UserDao;
import com.ouli.crm.settings.domain.User;
import com.ouli.crm.settings.servce.UserServce;
import com.ouli.crm.utils.DateTimeUtil;
import com.ouli.crm.utils.SqlSessionUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserServceImpl implements UserServce {

    private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);

    public User login(String loginAct, String loginPwd,String ip) throws LoginException {
        Map<String,String> map = new HashMap<String, String>();
        map.put("loginAct",loginAct);
        map.put("loginPwd",loginPwd);
        User user =userDao.login(map);
        if (user == null) {
            throw new LoginException("账号密码错误");
        }

        String expireTime = user.getExpireTime();
        String curentTime = DateTimeUtil.getSysTime();
        if (expireTime.compareTo(curentTime)<0) {
            throw new LoginException("账号已失效");
        }

        String lockState = user.getLockState();
        if ("0".equals(lockState)) {
            throw new LoginException("账号已锁定");
        }

        String allowIps = user.getAllowIps();
        if (!(allowIps.contains(ip))) {
            throw  new LoginException("ip地址受限");
        }

        return user;
    }

    public List<User> getUserList() {

        List<User> uList =userDao.getUserList();

        return uList;
    }
}
